package com.hpeng.gl.action;


import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hpeng.basic.service.AccountingSubjectManager;
import com.hpeng.gl.service.DepartmentService;
import com.hpeng.gl.service.StaffService;
import com.hpeng.sys.model.AccountingSubject;
import com.hpeng.sys.model.Department;
import com.hpeng.sys.model.Staff;
import com.hpeng.sys.service.Function;
import com.hpeng.util.Pagination;
import com.opensymphony.xwork2.ActionSupport;

@Component("staffAction")
@Scope("prototype")
public class StaffAction extends ActionSupport{
	private static final long serialVersionUID = 1L;
	@Resource
	private StaffService staffService;
	@Resource
	private DepartmentService departmentService;
	@Resource
	private Function function;
	private List<String> comefromList;
	@Resource
	// 会计科目
	private AccountingSubjectManager accountingSubjectManager;
	private List<AccountingSubject> accountingSubjectList;
	private List<Department> departments;
	private List<Staff> staffs;
	private Staff staff;      
	private String staid;
	private String depid;
	private String msg;
	private String FN;
	
	//根据条件进行查询
	private String condition;	//条件类型
	private String conditionValue;	//条件值

	//分页
	private Pagination<Staff> pagination;
	private int page;	//当前页数
	public String pageLink;
	

	//查询所有的员工信息 
	public String findAllStaff(){
		staffs = staffService.findStaff();
		return "success";
	}   
	
	//根据员工编号得到该员工的信息
	public String findStaff(){
		staff = staffService.findStaffByStaid(staid);
		departments = departmentService.findDepartment();
		return "success";
	}
	
	
	//新增员工信息
	public String addStaff(){
//		System.out.println("经办人"+staff.getJbr());
		try {//得到所有的员工的编号 进行比较  取出最大的 并在此基础上加1   这样就是新员工的编号了---手动去生成新加员工的信息
//		int num[] = new int[staffService.findStaff().size()];
//		for(int i=0;i<staffService.findStaff().size();i++){
//			num[i]=Integer.parseInt(1+staffService.findStaff().get(i).getStaid());
//		}
//		//使用冒泡排序得到最大的编号id
//		int max=0;
//		for(int i=0;i<num.length-1;i++){
//			for(int j=0;j<num.length-1-i;j++){
//				if(num[j]>num[j+1]){
//					max=num[j];			
//					num[j]=num[j+1];
//					num[j+1]=max;
//				}
//			}
//		}	
//		int id = num[num.length-1];//把最大值赋给id
//		String str = (++id)+"";
//		staff.setStaid(str.substring(1));
		staff.setStaid(function.getMaxStaffId());
		staff.setDepartment(departmentService.findDepartmentById(depid));
		staffService.addStaff(staff);
		} catch (RuntimeException e) {
			msg = getText("save.failure");
		}if (msg == null || "".equals(msg.trim())) {
			msg = getText("save.success");
		}
		return "success";
	}
	
	//批量删除员工信息   同时可进行单条数据删除    
	public String deleteStaff(){
		try {
		String[] stringArray = staid.split(",");
		for(int i=0;i<stringArray.length;i++){
			staffService.delStaff(staffService.findStaffByStaid(stringArray[i]));
		}
		} catch (RuntimeException e) {
			msg = getText("del.failure");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("del.success");
		}
		return "success";
	}
	
	//删除员工信息
	public String delStaff(){
		try {
		staff = staffService.findStaffByStaid(staid);
		staffService.delStaff(staff);	
		} catch (RuntimeException e) {
			msg = getText("del.failure");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("del.success");
		}
		return "success";
	}
	
	//更新员工信息
	public String updateStaff(){
		try {		
		Department dep = new Department();
		dep = departmentService.findDepartmentById(depid);
		staff.setDepartment(dep);
		staffService.updateStaff(staff);
		} catch (RuntimeException rx) {
			msg = getText("update.failure");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("update.success");
		}
		return "success";
	}
	
	//分页显示员工信息
	public String searchStaff(){
		String[] hql = {" from Staff sta,Department dep where sta in elements(dep.staffs) and dep.depstatus='A'"};
		String order = " order by staid asc";
		setPagination(staffService.getPagination(page, hql, null, order));
		System.out.println("得到的数据dataList的长度为："+pagination.getDataList().size());
		pageLink=pagination.getPageLink("searchStaff.action");
		return "success";
	}
	
	//查询所有部门信息
	public String findAllDepartment(){
		accountingSubjectList = accountingSubjectManager.findAccountingSubject();
		comefromList=function.getAssetComefrom();
		departments = departmentService.findDepartment();
		return "success";
	}
	
	//根据条件进行查询员工信息
	public String queryStaff(){
		try {
			conditionValue=new String(conditionValue.getBytes("ISO8859-1"),"UTF-8");
			String append="";
			if("ygbh".equals(condition)){
				append = " and staid like '%'||?||'%' ";
			}else if("ygxm".equals(condition)){
				append = " and staname like '%'||?||'%' ";
			}else if("ygzw".equals(condition)){
				append = " and staposition like '%'||?||'%' ";
			}
			String[] hql = {" from Staff sta,Department dep where sta in elements(dep.staffs) and dep.depstatus='A'",append};
			String order = " order by staid asc";
			setPagination(staffService.getPagination(page, hql,("".equals(append)?null:new String[]{conditionValue}), order));
			pageLink=pagination.getPageLink("queryStaff.action?condition="+condition+"&conditionValue="+conditionValue);
		} catch (UnsupportedEncodingException e) {
			System.out.println(e.getMessage());
		}
		return "success";
	}
	
//点击员工树上   分别对不同部门下的员工进行查询
	 public String queryStaffByDepartment(){
		 String[] hql = {" from Staff sta,Department dep where dep.depid=? and sta in elements(dep.staffs)" };
		 String order = " ";
		 setPagination(staffService.getPagination(page, hql,new String[]{depid}, order));
		 pageLink=pagination.getPageLink("queryStaffByDepartment.action?depid="+depid+"");
		 return "success";
	 }

/*******set  get   构造方法*********/	
	public List<Staff> getStaffs() {
		return staffs;
	}
	public void setStaffs(List<Staff> staffs) {
		this.staffs = staffs;
	}
	public Staff getStaff() {
		return staff;
	}
	public void setStaff(Staff staff) {
		this.staff = staff;
	}
	public Pagination<Staff> getPagination() {
		return pagination;
	}
	public void setPagination(Pagination<Staff> pagination) {
		this.pagination = pagination;
	}
	public String getStaid() {
		return staid;
	}
	public void setStaid(String staid) {
		this.staid = staid;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public List<Department> getDepartments() {
		return departments;
	}
	public void setDepartments(List<Department> departments) {
		this.departments = departments;
	}
	public String getDepid() {
		return depid;
	}
	public void setDepid(String depid) {
		this.depid = depid;
	}
	public String getCondition() {
		return condition;
	}
	public void setCondition(String condition) {
		this.condition = condition;
	}
	public String getConditionValue() {
		return conditionValue;
	}
	public void setConditionValue(String conditionValue) {
		this.conditionValue = conditionValue;
	}
	public String getPageLink() {
		return pageLink;
	}

	public void setPageLink(String pageLink) {
		this.pageLink = pageLink;
	}
	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getFN() {
		return FN;
	}

	public void setFN(String fN) {
		FN = fN;
	}
	public List<String> getComefromList() {
		return comefromList;
	}

	public void setComefromList(List<String> comefromList) {
		this.comefromList = comefromList;
	}

	public List<AccountingSubject> getAccountingSubjectList() {
		return accountingSubjectList;
	}

	public void setAccountingSubjectList(
			List<AccountingSubject> accountingSubjectList) {
		this.accountingSubjectList = accountingSubjectList;
	}
	
}

